<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
	//添加分页
   	public function getAdd()
   	{
   		// 查询所有分类
   		$cates = self::CateList();

   		return view('admin.cate.add',['cates'=>$cates]);	
   	}
   	public function postInsert(Request $request)
   	{  		
   		$data = $request->only(['name','pid']);   		
   		//判断pid的值
   		if($data['pid']==0)
   		{
	   		$data['path'] = '0';
   		}else{
   			//根据皮带查询path
   			$res = DB::table('cate')->where('id',$data['pid'])->first();
   			$data['path'] = $res->path.','.$data['pid'];
   		}
	   		$data['status'] = 1;
   		//执行数据插入
   		$res = DB::table('cate')->insert($data);
   		if($res)
   		{
   			return redirect('admin/cate/add')->with('success','分类添加成功');
   		}else{
   			return back()->with('error','分类添加失败');
   		}
   	}
   	//分类列表
   	public function getList()
   	{

   		// 查询所有
   		$cates = self::CateList();

   		foreach ($cates as $k => $v)
   		{
   			if($v->pid != 0)
   			{
   				$res = DB::table('cate')->where('id',$v->pid)->first();
   				$cates[$k]->path = $res->name;
   			}else{
   				$cates[$k]->path = '顶级分类';
   			}
   		}
   		// $cates = DB::table('cate')->paginate(5);
   		// dd($cates);
   		return view('admin.cate.list',['cates'=>$cates]);
   	}
   	// 按照格式查询  分类
   	static public function CateList()
   	{
   		// 查询所有
   		$cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
   		foreach($cates as $k=>$v)
   		{
   			//把字符串变成数组
   			$arr = explode(',',$v->path);
   			//获取长度
   			$len = count($arr)-1;
   			$cates[$k]->name = str_repeat('|==',$len).$v->name;
   		}
   		return $cates;
   	}
   	//删除分类
   	public function postDelete(Request $request)
   	{
   		// 获取id
   		$id = $request->input('id');
   		// dd($id);
   		// 检测分类下是否还有子类
   		$res = DB::table('cate')->where('pid',$id)->count();
   		// dd($res);
   		if($res > 0)
   		{
   			// 给ajax返回数据
   			return 1;
   		}
   		// 执行
   		$res = DB::table('cate')->where('id',$id)->delete();
   		if($res)
   		{
   			echo '删除成功';
   		}else{
   			echo '删除失败';
   		}
   	}
   	// 修改分类
   	public function getEdit($id)
   	{
   		//根据列表查询
   		$cate = DB::table('cate')->where('id',$id)->first();
   		$cates = self::CateList();
   		//解析模板
   		return view('admin.cate.edit',['cateinfo'=>$cate,'cates'=>$cates]);
   	}
   	// 分类数据修改
   	public function postUpdate(Request $request)
   	{
   		// dd($request->all());
   		$id = $request->input('id');
   		// 判断是否修改了分类
   		if($request->input('pid') != $request->input('xpid'))
   		{
   			// 要修改分类 查询
   			$res = DB::table('cate')->where('pid',$id)->count();
   			if($res > 0)
   			{
   				// 柚子分类不可修改
   				return back()->with('error','有子分类不可修改');
   			}
   		}
   		// 可以修改
   		$data = $request->only(['name','pid']);
   		//判断pid的值
   		if($data['pid']==0)
   		{
	   		$data['path'] = '0';
   		}else{
   			//根据皮带查询path
   			$res = DB::table('cate')->where('id',$data['pid'])->first();
   			$data['path'] = $res->path.','.$data['pid'];
   		}
   		// 执行
	   	$res = DB::table('cate')->where('id',$id)->update($data);
   		if($res)
   		{
   			return redirect('/admin/cate/list')->with('success','修改成功');
   		}else{
   			return back()->with('error','分类修改失败');
   		}
   	}
}
?>